﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace BanmaytinhDAL
{
    public class HangDAL : SQLDataProvider
    {
        public HangInfo Hang_GetById(string Mahang)

        { 
            BanmaytinhDAL.HangInfo list = new HangInfo();
            using (SqlCommand dbCmd = new SqlCommand("sp_Hang_GetById", GetConnection()))
			{
				BanmaytinhDAL.HangInfo obj = new BanmaytinhDAL.HangInfo();
				dbCmd.CommandType = CommandType.StoredProcedure;
				dbCmd.Parameters.Add(new SqlParameter("@Id", Mahang));
				SqlDataReader dr = dbCmd.ExecuteReader();
				if (dr.HasRows)
				{
					while (dr.Read())
					{
						list=obj.HangIDataReader(dr);
					}
				}
                dr.Close();
                obj = null;
			}
			return list;
        }
        public List<HangInfo> Hang_GetByAll()
        {
            List<BanmaytinhDAL.HangInfo> list = new List<BanmaytinhDAL.HangInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Hang_GetByAll", GetConnection()))
            {
                BanmaytinhDAL.HangInfo obj = new BanmaytinhDAL.HangInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.HangIDataReader(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return list;
        }

        public bool Insert(HangInfo obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_Hang_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Id", (obj.Mahang == null) ? String.Empty : obj.Mahang);
                    cmd.Parameters.AddWithValue("@Tenhang", (obj.Tenhang == null) ? String.Empty : obj.Tenhang);
                    cmd.Parameters.AddWithValue("@Maloai", (obj.Maloai == null) ? String.Empty : obj.Maloai);
                    cmd.Parameters.AddWithValue("@Mahangsanxuat", (obj.Mahangsanxuat == null) ? String.Empty : obj.Mahangsanxuat);
                    cmd.Parameters.AddWithValue("@Makho", (obj.Makho == null) ? String.Empty : obj.Makho);
                    cmd.Parameters.AddWithValue("@Madonvitinh", (obj.Madonvitinh == null) ? String.Empty : obj.Madonvitinh);
                    cmd.Parameters.AddWithValue("@Thoigianbaohanh", (obj.Thoigianbaohanh == null) ? String.Empty : obj.Thoigianbaohanh);
                    cmd.Parameters.AddWithValue("@Thutuhienthi", (obj.Thutuhienthi == null) ? "0" : obj.Thutuhienthi);
                    cmd.Parameters.AddWithValue("@Tieude", (obj.Tieude == null) ? String.Empty : obj.Tieude);
                    cmd.Parameters.AddWithValue("@Motasoluoc", (obj.Motasoluoc == null) ? String.Empty : obj.Motasoluoc);
                    cmd.Parameters.AddWithValue("@Tukhoatimkiem", (obj.Tukhoatimkiem == null) ? String.Empty : obj.Tukhoatimkiem);
                    cmd.Parameters.AddWithValue("@Serial", (obj.Serial == null) ? String.Empty : obj.Serial);
                    cmd.Parameters.AddWithValue("@Anh", (obj.Anh == null) ? String.Empty : obj.Anh);
                    cmd.Parameters.AddWithValue("@Hienthi", (obj.Hienthi == null) ? String.Empty : obj.Hienthi);

                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(HangInfo obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_Hang_Update";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Id", (obj.Mahang == null) ? String.Empty : obj.Mahang);
                    cmd.Parameters.AddWithValue("@Tenhang", (obj.Tenhang == null) ? String.Empty : obj.Tenhang);
                    cmd.Parameters.AddWithValue("@Maloai", (obj.Maloai == null) ? String.Empty : obj.Maloai);
                    cmd.Parameters.AddWithValue("@Mahangsanxuat", (obj.Mahangsanxuat == null) ? String.Empty : obj.Mahangsanxuat);
                    cmd.Parameters.AddWithValue("@Makho", (obj.Makho == null) ? String.Empty : obj.Makho);
                    cmd.Parameters.AddWithValue("@Madonvitinh", (obj.Madonvitinh == null) ? String.Empty : obj.Madonvitinh);
                    cmd.Parameters.AddWithValue("@Thoigianbaohanh", (obj.Thoigianbaohanh == null) ? String.Empty : obj.Thoigianbaohanh);
                    cmd.Parameters.AddWithValue("@Thutuhienthi", (obj.Thutuhienthi == null) ? "0" : obj.Thutuhienthi);
                    cmd.Parameters.AddWithValue("@Tieude", (obj.Tieude == null) ? String.Empty : obj.Tieude);
                    cmd.Parameters.AddWithValue("@Motasoluoc", (obj.Motasoluoc == null) ? String.Empty : obj.Motasoluoc);
                    cmd.Parameters.AddWithValue("@Tukhoatimkiem", (obj.Tukhoatimkiem == null) ? String.Empty : obj.Tukhoatimkiem);
                    cmd.Parameters.AddWithValue("@Serial", (obj.Serial == null) ? String.Empty : obj.Serial);
                    cmd.Parameters.AddWithValue("@Anh", (obj.Anh == null) ? String.Empty : obj.Anh);
                    cmd.Parameters.AddWithValue("@Hienthi", (obj.Hienthi == null) ? String.Empty : obj.Hienthi);

                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(string Mahang)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "sp_Hang_Delete";
                    cmd.Parameters.AddWithValue("@Id", Mahang);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


    }

}
